package www.wen.com.common.lib.data.network

import android.app.Application
import com.franmontiel.persistentcookiejar.ClearableCookieJar
import okhttp3.Cache
import okhttp3.Interceptor
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.converter.moshi.MoshiConverterFactory
import retrofit2.converter.scalars.ScalarsConverterFactory
import www.wen.com.common.BuildConfig
import www.wen.com.common.lib.util.BASIC_URL
import java.io.File
import java.util.concurrent.TimeUnit

/**
 * @author WEN
 * @Description:
 * @date 2020/11/20 0:44
 */

class ApiServiceClient {

    companion object {

        private lateinit var apiService: ApiService

        operator fun invoke(
                connectivityInterceptor: Interceptor,
                cookieJar: ClearableCookieJar,
                application: Application
        ): ApiService {

            val requestInterceptor = Interceptor { chain ->

                val url = chain.request()
                        .url
                        .newBuilder()
                        .build()
                val request = chain.request()
                        .newBuilder()
                        .url(url)
                        .build()
                return@Interceptor chain.proceed(request)
            }

            val httpCacheDirectory = File(application.cacheDir, "responses")
            val cacheSize = 10 * 1024 * 1024L // 10 MiB
            val cache = Cache(httpCacheDirectory, cacheSize)

            val okHttpClient = OkHttpClient
                    .Builder()
                    .addInterceptor(AddParamsInterceptorImpl)
                    .addInterceptor(requestInterceptor)
                    .addInterceptor(
                            HttpLoggingInterceptor().setLevel(
                                    if (BuildConfig.DEBUG)
                                        HttpLoggingInterceptor.Level.BODY
                                    else
                                        HttpLoggingInterceptor.Level.NONE
                            )
                    )
//                    .addInterceptor(CacheInterceptorImpl)
                    .connectTimeout(10, TimeUnit.SECONDS)
                    .readTimeout(10, TimeUnit.SECONDS)
                    .writeTimeout(10, TimeUnit.SECONDS)
//                    .addInterceptor(connectivityInterceptor)
                    .cache(cache)
                    .cookieJar(cookieJar)
                    .build()

            apiService = Retrofit.Builder()
                    .baseUrl(BASIC_URL)
                    .client(okHttpClient)
                    .addConverterFactory(ScalarsConverterFactory.create())
                    .addConverterFactory(GsonConverterFactory.create())
                    .addConverterFactory(MoshiConverterFactory.create())
                    .build()
                    .create(ApiService::class.java)
            return apiService
        }
    }

}

